package dbgroup02.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import dbgroup02.bean.Baggage;
import dbgroup02.bean.BoardingPass;
import dbgroup02.bean.CheckInInfo;
import dbgroup02.bean.Flight;
import dbgroup02.bean.FlightRoute;
import dbgroup02.bean.FlightTicket;
import dbgroup02.bean.Passenger;
import dbgroup02.bean.Ticket;
import dbgroup02.dao.BaggageDao;
import dbgroup02.dao.BoardingPassDao;
import dbgroup02.dao.CheckInInfoDao;
import dbgroup02.dao.FlightDao;
import dbgroup02.dao.FlightRouteDao;
import dbgroup02.dao.FlightTicketDao;
import dbgroup02.dao.PassengerDao;
import dbgroup02.dao.SeatDao;
import dbgroup02.dao.TicketDao;

public class BoardingPassesController implements Controller {

	private TicketDao ticketDao;
	private PassengerDao passengerDao;
	private FlightTicketDao flightTicketDao;
	private BoardingPassDao boardingPassDao;
	private FlightDao flightDao;
	private FlightRouteDao flightRouteDao;
	private CheckInInfoDao checkInInfoDao;
	private SeatDao seatDao;
	private BaggageDao baggageDao;
	
	public void setBaggageDao(BaggageDao baggageDao) {
		this.baggageDao = baggageDao;
	}
	
	public void setSeatDao(SeatDao seatDao) {
		this.seatDao = seatDao;
	}
	
	public void setTicketDao(TicketDao ticketDao) {
		this.ticketDao = ticketDao;
	}

	public void setPassengerDao(PassengerDao passengerDao) {
		this.passengerDao = passengerDao;
	}

	public void setFlightTicketDao(FlightTicketDao flightTicketDao) {
		this.flightTicketDao = flightTicketDao;
	}
	
	
	public void setBoardingPassDao(BoardingPassDao boardingPassDao) {
		this.boardingPassDao = boardingPassDao;
	}
	
	public void setFlightDao(FlightDao flightDao) {
		this.flightDao = flightDao;
	}
	
	public void setFlightRouteDao(FlightRouteDao flightRouteDao) {
		this.flightRouteDao = flightRouteDao;
	}
	
	public void setCheckInInfoDao(CheckInInfoDao checkInInfoDao) {
		this.checkInInfoDao = checkInInfoDao;
	}
	
	@Override
	@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int idTicket = Integer.parseInt(request.getParameter("idTicket"));
		Ticket ticket = ticketDao.getTicket(idTicket);
				
		Passenger passenger = passengerDao.getPassenger(ticket.getIdPassenger());
		
		List<FlightTicket> flightTickets = flightTicketDao.getFlightTickets(ticket.getIdTicket());
		
		List<BoardingPass> boardingPasses = new ArrayList<BoardingPass>();
		List<Flight> flights = new ArrayList<Flight>();
		List<FlightRoute> flightRoutes = new ArrayList<FlightRoute>();
		
		CheckInInfo checkInfo = checkInInfoDao.getCheckInfoByFlight(flightTickets.get(0).getIdFlight());
		Baggage baggage = baggageDao.getBaggageByCheckInInfo(checkInfo.getIdCheckInInfo());
		
		for (FlightTicket flightTicket: flightTickets) {
			boardingPasses.add(boardingPassDao.getBoardingPass(flightTicket.getIdBoardingPass()));
			
			Flight flight = flightDao.getFlight(flightTicket.getIdFlight());
			flights.add(flight);
			
			flightRoutes.add(flightRouteDao.getFlightRoute(flight.getIdFlightRoute()));
		}
		
		
		
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("passenger", passenger);
		model.put("boardingPasses", boardingPasses);
		model.put("flights", flights);
		model.put("flightRoutes", flightRoutes);
		model.put("baggage", baggage);
		
		return new ModelAndView("boardingpasses", model);
	}

}
